/*
 * Copyright (C) 2004, Velocityme Partnership
 * UserValueActionForm.java
 *
 * Created on 25 September 2004, 04:45
 */

package com.velocityme.www.actionforms.nodeactionforms;

import com.velocityme.entity.User;
import com.velocityme.session.KeyLocal;
import com.velocityme.session.PersonLocal;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author  Robert
 */
public class UserActionForm extends NodeActionForm {
    
    private Long m_personId;
    private String m_password = new String();
    private String m_repeatPassword = new String();

    public void extractUser(KeyLocal key, User user) {
        extractNode(user);
        user.setPerson(lookupPersonBean().findPerson(key, m_personId));
    }

    @Override
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if (!m_password.equals(m_repeatPassword)) {
            errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage("error.both.passwords.equal"));
        }
        return errors;
    }
    
    public Long getPersonId() {
        return m_personId;
    }
    
    public void setPersonId(Long personId) {
        m_personId = personId;
    }
    
    public String getPassword() {
        return m_password;
    }
    
    public void setPassword(String password) {
        m_password = password;
    }
    
    public String getRepeatPassword() {
        return m_repeatPassword;
    }
    
    public void setRepeatPassword(String repeatPassword) {
        m_repeatPassword = repeatPassword;
    }

    private PersonLocal lookupPersonBean() {
        try {
            Context c = new InitialContext();
            return (PersonLocal) c.lookup("java:comp/env/PersonBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
    
}
